﻿using BW.Mes.WinForms.APi;
using BW.Mes.WinForms.Dto;
using BW.Mes.WinForms.Properties;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BW.Mes.WinForms.WareHouse.SuppliedMaterials
{
    public partial class GetSuppliedMaterialsForm : DevExpress.XtraEditors.XtraForm
    {
        string baseUrl = Settings.Default.WareHouseUrl;
        private ApiPaing<ShowSuppliedMaterial> showexternal;
        public GetSuppliedMaterialsForm()
        {
            InitializeComponent();
            // 注册分页控件事件
            userControl11.ClickPageButtonEvent += UserControl1_ClickPageButtonEvent;
            userControl11.ChangedPageSizeEvent += UserControl1_ChangedPageSizeEvent;
            userControl11.JumpPageEvent += UserControl1_JumpPageEvent;
            this.Load += SuppliedMaterialsForm_Load;
        }

        private async void SuppliedMaterialsForm_Load(object sender, EventArgs e)
        {
            await GetSuppliedMaterials();
            DisplayPagedData();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 获取来料标签打印记录
        /// </summary>
        public  async Task GetSuppliedMaterials()
        {
            var result = await HttpClientHepler.RequsetUrl(RequestType.get, MethodGolab.SHOW_SUPPLIEDMATERISLS);
            //反序列化
             showexternal = JsonConvert.DeserializeObject<ApiPaing<ShowSuppliedMaterial>>(result);
            if (showexternal.code == 200)
            {
                this.gridControl1.DataSource = showexternal.pageDatas;
            }

            //// 设置总记录数和总页数
            userControl11.TotalRows = showexternal.totalCount;
            userControl11.TotalPages = (int)Math.Ceiling((double)showexternal.totalCount / userControl11.PageSize);

            // 更新页码信息标签
            userControl11.PageInfo.Text = $"第{userControl11.CurrentPage}/{userControl11.TotalPages}页";


            // 确保清除旧数据源并重新设置
            gridControl1.DataSource = null;
            gridControl1.DataSource = showexternal;

            // 确保网格视图显示数据
            gridView1.PopulateColumns();
            gridView1.BestFitColumns();
            gridView1.RefreshData();

        }

        // 显示分页后的数据
        private void DisplayPagedData()
        {
            if (showexternal == null || showexternal.totalCount == 0)
                return;

            int startIndex = (userControl11.CurrentPage - 1) * userControl11.PageSize;
            int count = Math.Min(userControl11.PageSize, showexternal.totalCount - startIndex);

            if (startIndex < 0 || count <= 0)
                return;

            // 获取当前页的数据
            var pagedData = showexternal.pageDatas.Skip(startIndex).Take(count).ToList();

            // 绑定到GridControl
            gridControl1.DataSource = null;
            gridControl1.DataSource = pagedData;

            // 刷新网格视图
            gridView1.BestFitColumns();
            gridView1.RefreshData();
        }

        // 页码按钮点击事件处理
        private void UserControl1_ClickPageButtonEvent(int currentPage)
        {
            DisplayPagedData();
        }

        // 每页显示条数改变事件处理
        private void UserControl1_ChangedPageSizeEvent()
        {
            // 重新计算总页数
            userControl11.TotalPages = (int)Math.Ceiling((double)showexternal.totalCount / userControl11.PageSize);
            userControl11.PageInfo.Text = $"第{userControl11.CurrentPage}/{userControl11.TotalPages}页";
            DisplayPagedData();
        }

        // 跳转页面事件处理
        private void UserControl1_JumpPageEvent(int jumpPage)
        {
            DisplayPagedData();
        }







    }
}